﻿
namespace HelperLibrary {

    public static class Convert {

        public static class DateTime {
            public static System.DateTime FromUnixTime ( System.Int64 unixTime ) {
                var epoch = new System.DateTime( 1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc );
                return epoch.AddSeconds( unixTime );

            }

            public static System.String CurrentUnixTime () {
                System.TimeSpan t = System.DateTime.UtcNow - new System.DateTime( 1970, 1, 1 );
                return ( ( System.Int64 ) t.TotalSeconds ).ToString();

            }

            public static System.Int64 ToUnixTime ( System.DateTime date ) {
                System.DateTime epoch = new System.DateTime( 1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc );
                return System.Convert.ToInt64( ( date.ToUniversalTime() - epoch ).TotalSeconds );
            }

            public static System.DateTime getDateTimeFromJsonString ( System.String datetime_string ) {
                System.String[] date = datetime_string.Split( ": -".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries );
                System.DateTime datetime = new System.DateTime(
                                System.Int32.Parse( date[0] ),
                                System.Int32.Parse( date[1] ),
                                System.Int32.Parse( date[2] ),
                                System.Int32.Parse( date[3] ),
                                System.Int32.Parse( date[4] ),
                                System.Int32.Parse( date[5] )
                                );

                return datetime;
            }

        }

        public static class String {

            public static byte[] ToByteArray ( System.String hex ) {
                //System.Int32 NumberChars = hex.Length / 2;
                //byte[] bytes = new byte[NumberChars];
                //using ( var sr = new System.IO.StringReader( hex ) ) {
                //    for ( System.Int32 i = 0; i < NumberChars; i++ )
                //        bytes[i] =
                //          System.Convert.ToByte(
                //            new System.String(
                //                new System.Char[2] { 
                //                    ( System.Char ) sr.Read(), 
                //                    ( System.Char ) sr.Read() 
                //                }
                //            ),
                //            16
                //         );
                //}
                //return bytes;

                if ( ( hex.Length % 2 ) == 1 ) hex += '0';
                byte[] bytes = new byte[hex.Length / 2];
                for ( int i = 0; i < hex.Length; i += 2 ) {
                    bytes[i / 2] = System.Convert.ToByte( hex.Substring( i, 2 ), 16 );
                }
                return bytes;

            }

            public static System.String ByteToString ( System.Byte[] buff ) {
                System.String sbinary = "";

                for ( System.Int32 i = 0; i < buff.Length; i++ ) {
                    sbinary += buff[i].ToString( "x2" ); // hex format
                }

                return ( sbinary );
            }

            public static System.Int32[] indexOfAny_String ( System.String str, System.String s ) {
                System.Collections.Generic.List<System.Int32> index = new System.Collections.Generic.List<System.Int32>();
                System.Int32 temp;
                System.Boolean found;

                for ( System.Int32 i = 0; i < str.Length; i++ ) {
                    temp = -1;
                    found = false;

                    if ( ( i + s.Length ) >= str.Length ) {
                        break;
                    }

                    if ( str[i] == s[0] ) {
                        temp = i;

                    } else {
                        continue;
                    }

                    for ( System.Int32 j = 1; j < s.Length; j++ ) {
                        if ( str[i + j] != s[j] ) {
                            found = false;
                            break;
                        }
                        found = true;
                    }

                    if ( !found ) {
                        continue;
                    }

                    index.Add( temp );
                    i += s.Length - 1;

                }

                return index.ToArray();
            }

            public static System.Byte[] GetBytes ( System.String s ) {
                System.Byte[] retval = new System.Byte[s.Length];
                for ( System.Int32 ix = 0; ix < s.Length; ++ix ) {
                    System.Char ch = s[ix];
                    if ( ch <= 0x7f ) retval[ix] = ( System.Byte ) ch;
                    else retval[ix] = ( System.Byte ) '?';
                }

                return retval;
            }



        }

    }

    public static class Parse {
        private static System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CurrentCulture;

        /// <summary>
        /// Provides information about the current culture . The information includes the names for the culture, the
        /// writing system, the calendar used, and formatting for dates and sort strings.
        /// If you set this value, it will set all culture value for Int, Float and Double
        /// </summary>
        public static System.Globalization.CultureInfo Culture {
            get {
                return culture;
            }
            set {
                culture = value;
                Int.Culture = value;
                Float.Culture = value;
                Double.Culture = value;
            }
        }

        public static class Int {
            private static System.Globalization.CultureInfo _culture;

            static Int () {
                _culture = culture;
            }

            /// <summary>
            /// Provides information about a specific culture . The information includes the names for the culture, the
            /// writing system, the calendar used, and formatting for dates and sort strings.
            /// This value will be the same as the parent Class "Parse", if you do not override it.
            /// </summary>
            public static System.Globalization.CultureInfo Culture {
                get {
                    return _culture;
                }
                set {
                    _culture = value;
                }
            }

            /// <summary>
            /// Converts the string representation of a number to its 32-bit signed integer
            /// equivalent.
            /// </summary>
            /// <param name="s">A string containing a number to convert.</param>
            /// <returns></returns>
            public static System.Int32 Parse ( System.String s ) {
                return System.Int32.Parse( s, _culture );
            }

        }

        public static class Float {
            private static System.Globalization.CultureInfo _culture;

            static Float () {
                _culture = culture;
            }

            /// <summary>
            /// Provides information about a specific culture . The information includes the names for the culture, the
            /// writing system, the calendar used, and formatting for dates and sort strings.
            /// This value will be the same as the parent Class "Parse", if you do not override it.
            /// </summary>
            public static System.Globalization.CultureInfo Culture {
                get {
                    return _culture;
                }
                set {
                    _culture = value;
                }
            }

            /// <summary>
            /// Converts the string representation of a number to its single-precision floating-point
            /// number equivalent.
            /// </summary>
            /// <param name="s">A string that contains a number to convert.</param>
            /// <returns></returns>
            public static System.Single Parse ( System.String s ) {
                return System.Single.Parse( s, culture );
            }

        }

        public static class Double {
            private static System.Globalization.CultureInfo _culture;

            static Double () {
                _culture = culture;
            }

            /// <summary>
            /// Provides information about a specific culture . The information includes the names for the culture, the
            /// writing system, the calendar used, and formatting for dates and sort strings.
            /// This value will be the same as the parent Class "Parse", if you do not override it.
            /// </summary>
            public static System.Globalization.CultureInfo Culture {
                get {
                    return _culture;
                }
                set {
                    _culture = value;
                }
            }

            /// <summary>
            /// Converts the string representation of a number in a specified culture-specific
            /// format to its double-precision floating-point number equivalent.
            /// </summary>
            /// <param name="s">A string that contains a number to convert.</param>
            /// <returns></returns>
            public static System.Double ParseDouble ( System.String s ) {
                return System.Double.Parse( s, culture );
            }

        }



    }

    public static class Array {

        public static T [] MergeTwoArrays <T> (T [] array1, T [] array2 ) {

            T [] array = new T[array1.Length + array2.Length];
            System.Array.Copy( array1, 0, array, 0, array1.Length );
            System.Array.Copy( array2, 0, array, array1.Length, array2.Length );

            return array;

        }

    }


}

